export default {
    state: null,
    subscriptions: { // 在模型加入到仓库中后立即运行。
        syncLocalstorage({ dispatch }) {
            const loginId = localStorage.getItem('loginId')
            if (loginId) {
                dispatch({ type: 'setLoginUser', payload: loginId })
            }
        }
    },
    reducers: {
        setLoginUser(state, { type, payload }) {
            return payload
        }
    },
    effects: {
        *login({ payload: { loginId, loginPwd } }, { put, delay }) {
            if (loginId === 'admin' && loginPwd === '123456') {
                yield put({ type: 'setLoginUser', payload: loginId })
                localStorage.setItem('loginId', loginId)
                return true
            }
        },
        *logout(action, { put }) {
            yield put({ type: 'setLoginUser', payload: null })
            localStorage.setItem('loginId', "")
        }
    }

}